package _18线程安全问题_买票案例;

public class Ticket implements Runnable{
    private int total=100;
    static Object obj = new Object();
    @Override
    public void run() {


        while (true) {

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            String name = Thread.currentThread().getName();
            synchronized (obj){
                System.out.println(name+"抢到锁了，开始购票");
                if (total > 0) {
                    total = total - 1;
                    System.out.println(name + "购票成功，剩余票数为：" + total);
                }else {
                    System.out.println("没票了，下次再来");
                    break;
                }
            }




        }
    }

}
